package org.sccba.demo01;

import java.io.File;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.HashMap;
import java.util.Map;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateExceptionHandler;

public class Test {

	public static void main(String[] args) throws Exception {
		
		//创建一个配置类
		Configuration cfg = new Configuration(Configuration.VERSION_2_3_29);
		
		cfg.setDirectoryForTemplateLoading(new File("G:\\EclipseWorkspaces\\freemarker\\demo01\\src\\resource\\templates"));
		
		cfg.setDefaultEncoding("UTF-8");
        cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
        cfg.setLogTemplateExceptions(false);
        cfg.setWrapUncheckedExceptions(true);
        cfg.setFallbackOnNullLoopVariable(false);
		
		//创建一个模型数据
        Map root = new HashMap();
        root.put("user", "Big Joe");
        Product latest = new Product();
        latest.setUrl("products/greenmouse.html");
        latest.setName("green mouse");
        root.put("latestProduct", latest);
		
        //获得模型
        Template temp = cfg.getTemplate("test.ftlh");
        
        Writer out = new OutputStreamWriter(System.out);
        //将模型和数据整合
        temp.process(root, out);
        
        
        
	}

}
